//
//  Stack.m
//  level02
//
//  Created by iGxnon on 2021/11/11.
//

#import "Stack.h"

@implementation Stack

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.stackFrameList = [NSMutableArray arrayWithCapacity:200];
    }
    return self;
}

- (StackFrame *) pop {
    if (self.size <= 0) {
        NSLog(@"无法弹出元素，该栈已经空了！");
        return NULL;
    }
    self.size --;
    if (self.size != 0) {
        self.top = self.stackFrameList[self.size - 1];
    }
    StackFrame *popFrame = self.stackFrameList[self.size];
    [self.stackFrameList removeObjectAtIndex:self.size];
    return popFrame;
}

- (void) push: (StackFrame *) stackFrame {
    if (self.size == 200) {
        NSLog(@"栈内存已经满了!");
        return;
    }
    self.size ++;
    [self.stackFrameList addObject:stackFrame];
    self.top = stackFrame;
}

- (StackFrame *) get {
    return self.stackFrameList[self.pointer];
}

- (void) next {
    if ([self hasNext]) {
        self.pointer ++;
    }
}

- (void) before {
    self.pointer = self.pointer - 1 < 0 ? 0 : self.pointer - 1;
}

- (bool) hasNext {
    return self.pointer < self.size - 1;
}

@end
